package rm.member.business.repository;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import rm.cq.common.core.enums.JudgeConfig;
import rm.cq.common.core.util.BaseDataMessageUtil;
import rm.member.business.domain.BO.ShoppingInfoBO;
import rm.member.business.domain.DO.ChangeDepositLgDetailDO;
import rm.member.business.domain.DO.MZSupplementOneDo;
import rm.member.business.domain.DO.MemberChangeInfoDetailDo;
import rm.member.business.domain.DO.OrderGoodsDetailListDo;
import rm.member.common.entity.mz.MZGiftConst;
import rm.member.common.enums.mz.DynamicObjectEnum;
import rm.member.common.enums.mz.UseWayEnum;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 *
 */
public class MZGiftRepository {

    /**
     * 查询所有数据，根据筛选条件
     */
    public List<DynamicObject> findAllBySelect(ShoppingInfoBO shoppingInfoBO){
        /*filters.and("1", QCP.equals, shoppingInfoBO.getMzSupplementOneDo().getChannelCode());*/
        //BO-->DO
        MZSupplementOneDo supplementOneDo = shoppingInfoBO.getMzSupplementOneDo();
        MemberChangeInfoDetailDo memberChangeInfoDetailDo = shoppingInfoBO.getMemberChangeInfoDetailDo();
        ChangeDepositLgDetailDO changeDepositLgDetailDO = shoppingInfoBO.getChangeDepositLgDetailDO();
        OrderGoodsDetailListDo orderGoodsDetailListDo = shoppingInfoBO.getOrderGoodsDetailListDo();

        //时间格式
        String date = supplementOneDo.getQueryDate();
        String[] riqi = date.split("T");
        String time = riqi[1].substring(0, 8);
        String datetime = riqi[0] + " " + time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        Date day = null;
        try {
            day = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //sql
        //时间
        QFilter filter = new QFilter(MZGiftConst.DAYPERIODSTARTDATE, QCP.less_equals,day );
        filter.and(MZGiftConst.DAYPERIODENDDATE, QCP.large_equals, day);
        //使用渠道
        QFilter filter1 = new QFilter(MZGiftConst.USEWAY, QCP.equals, supplementOneDo.getChannelCode());
        filter1.or(MZGiftConst.USEWAY, QCP.equals, "1");


        /*QFilter filter2 = new QFilter(MZGiftConst.MEMBERSCOPE, QCP.equals, "2");
        filter2.and("memberlevel", QCP.large_equals, memberChangeInfoDetailDo.getMemberLevel());
        filter2.or(MZGiftConst.MEMBERSCOPE, QCP.equals, "1");*/

        //活动状态  是否已审核
        QFilter filter3 = new QFilter("billstatus", QCP.equals, DynamicObjectEnum.EXAMINE.getValue());
        filter3.and(filter).and(filter1)/*.and(filter2)*/;

        Map<Object, DynamicObject> objectDynamicObjectMap = BusinessDataServiceHelper.loadFromCache(MZGiftConst.ENTITY_NAME, filter3.toArray());
        //全部符合条件活动方案
        List<DynamicObject> dynamicObjects = new ArrayList<>(objectDynamicObjectMap.values());
        return dynamicObjects;
    }

    //查询会员等级
    public long getMemberLevelId(String memberLevelCode){
        return BaseDataMessageUtil.getIdByCode(memberLevelCode, "rm_memberlevel");
    }


    /**
     * 根据活动ID查询活动方案
     * activityUid
     */
    public DynamicObject findActivityByUid(String activityUid){
        QFilter filters = new QFilter("id", QCP.equals, activityUid);
        Map<Object, DynamicObject> objectDynamicObjectMap = BusinessDataServiceHelper.loadFromCache(MZGiftConst.ENTITY_NAME, filters.toArray());
        List<DynamicObject> dynamicObjects = new ArrayList<>(objectDynamicObjectMap.values());
        if(dynamicObjects.size()>0){
            return  dynamicObjects.get(0);
        }
        return null;
    }
}
